<?
//Blog 0.0.7
class sym_blog extends symbiont{
    public function ___main($design="", $category='', $inpage=10, $limit=0, $type=0, $count=1000){
        global $kernel, $sym, $sql;
        if(!$kernel->symbiont_exists('category')){
            return;
        }
        //See category
        if($category!==''){
            //Get note
            if($kernel->link->param1){
                $note=$kernel->link->param1;
                $this->___note('', $category, $note);
            }
            //Get category
            else{
                $page=$kernel->link->modifier;
                $this->___category('', $category, $page, $inpage, $limit, $type, $count);
                
                $c=$sql->query_each("SELECT count(1) as count
                                    FROM `sym_blog` as b
                                        LEFT JOIN `paragraphs` as p ON b.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                    WHERE p.title!=''
                                    ".($limit?'LIMIT '.$limit:''), true);
                if($c>1&&$kernel->symbiont_exists('pager')){
                    $kernel->symbiont_add('pager');
                    $count=ceil($c/$inpage);
                    $sym->pager->___main('', $page, $count);
                }
            }
        }
        //See all categories
        else{
            //Get note
            if($kernel->link->param1!==''&&$kernel->link->param2!==''){
                $category=$kernel->link->param1;
                $note=$kernel->link->param2;
                $this->___note('', $category, $note);
            }
            //Get category
            elseif($kernel->link->param1!==''){
                $category=$kernel->link->param1;
                $page=$kernel->link->modifier;
                $this->___category('', $category, $page, $inpage, $limit, $type, $count);
                
                $c=$sql->query_each("SELECT count(1) as count
                                    FROM `sym_blog` as b
                                        LEFT JOIN `paragraphs` as p ON b.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                    WHERE p.title!=''
                                    ".($limit?'LIMIT '.$limit:''), true);
                if($c>1&&$kernel->symbiont_exists('pager')){
                    $kernel->symbiont_add('pager');
                    $count=ceil($c/$inpage);
                    $sym->pager->___main('', $page, $count);
                }
            }
            //Get all categories
            else{
                $page=$kernel->link->modifier;
                $this->___all_categories('', $page, $inpage, $limit, $type, $count);
                
                $c=$sql->query_each("SELECT count(1) as count
                                    FROM `sym_blog` as b
                                        LEFT JOIN `paragraphs` as p ON b.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                    WHERE p.title!=''
                                    ".($limit?'LIMIT '.$limit:''), true);
                if($c>1&&$kernel->symbiont_exists('pager')){
                    $kernel->symbiont_add('pager');
                    $count=ceil($c/$inpage);
                    $sym->pager->___main('', $page, $count);
                }
            }
        }
    }
    public function ___note($design, $category, $note){
        global $kernel, $sql, $sym;
        $kernel->symbiont_add('place');
        
        $design=$this->_check($design, 'note');
        $d=new design($design, 'blog', false);
        
        $category=data::word($category);
        $note=data::word($note);
        
        $blog=$sql->query_each("SELECT b.id, b.name, b.cover, c.name as category_name, p.title, p.content, cp.title as category_title FROM `sym_blog` as b LEFT JOIN `sym_category` as c ON b.category_id = c.id LEFT JOIN `paragraphs` as p ON b.paragraph_id = p.id AND p.lng_id = '".$kernel->lang->id."' LEFT JOIN `paragraphs` as cp ON c.paragraph_id=cp.id AND cp.lng_id='".$kernel->lang->id."' WHERE c.name='".$category."' AND b.name='".$note."'", true);
        
        if(is_array($blog)){
            $this->category_name=$blog['category_name'];
            $this->category_title=$blog['category_title'];
            $this->id=$blog['id'];
            $this->name=$blog['name'];
            $this->title=$blog['title']?$blog['title']:$blog['name'];
            $this->cover=$blog['cover'];
            $this->content=$blog['content'];
            
            $sym->place->add($this->category_title, $this->category_name);
            $sym->place->add($this->title, $this->name);
            $sym->place->___main('', true);
        }
        $d->inc();
    }
    public function ___category($design, $name, $page=1, $inpage=10, $limit=0, $type=0, $count=1000){
        global $sql, $kernel, $sym;
        $design=$this->_check($design, 'category');
        $d=new design($design, 'blog', false);
        
        $name=data::word($name);
        $page=data::is_uint($page)?$page:1;
        $inpage=data::is_uint($inpage)?$inpage:10;
        $limit=data::is_uint($limit)?$limit:0;
        $type=data::is_uint($type)?$type:0;
        $count=data::is_uint($count)?$count:1000;
        
        $category=$sql->query_each("SELECT c.id, p.title FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id." WHERE c.name = '".$name."' AND c.for = 'blog' LIMIT 1", true);
        
        $p=$page*$inpage;
        if($limit==0||$p<$limit){
            $lim="LIMIT ".($p-$inpage).", ".$p."";
        }
        elseif($p-$inpage<$limit){
            $lim="LIMIT ".($p-$inpage).", ".$limit."";
        }
        else{
            $lim='';
        }
        $notes=$sql->query_each("SELECT b.name, DATE_FORMAT(b.date, '%d.%m.%Y') as d, p.title, p.content, b.cover
                                FROM `sym_blog` as b
                                    LEFT JOIN `paragraphs` as p ON b.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                WHERE b.category_id = ".$category['id']." AND p.title!=''
                                ORDER BY date DESC
                                ".$lim);
        
        $this->notes='';
        $this->category_name=$name;
        $this->category_title=$category['title'];
        $sym->place->add($this->category_title, $this->category_name);
        $sym->place->___main('', true);
        $i=0;
        if($notes){
            foreach($notes as $note){
                if(!$note['title']) continue;
                $this->date=$note['d'];
                $this->name=$note['name'];
                $this->title=$note['title'];
                $this->cover=$note['cover'];
                if($type==0){
                    $this->content=data::html_remove($note['content'], $count);
                }
                elseif($type==1){
                    $this->content=data::paragraph($note['content'], $count);
                }
                else{
                    $this->content=$note['content'];
                }
                $this->i=$i;
                $this->notes.=design::run($this->note);
                $i++;
            }
        }
        
        $d->inc();
    }
    public function ___all_categories($design, $page=1, $inpage=10, $limit=0, $type=0, $count=1000){
        global $sql, $kernel, $sym;
        
        $page=data::is_uint($page)?$page:1;
        $inpage=data::is_uint($inpage)?$inpage:10;
        $limit=data::is_uint($limit)?$limit:0;
        $type=data::is_uint($type)?$type:0;
        $count=data::is_uint($count)?$count:1000;
        
        $p=$page*$inpage;
        if($limit==0||$p<$limit){
            $lim="LIMIT ".($p-$inpage).", ".$p."";
        }
        elseif($p-$inpage<$limit){
            $lim="LIMIT ".($p-$inpage).", ".$limit."";
        }
        else{
            $lim='';
        }
        
        $design=$this->_check($design, 'all_categories');
        $d=new design($design, 'blog', false);
        
        $notes=$sql->query_each("SELECT b.name, DATE_FORMAT(b.date, '%d.%m.%Y') as d, p.title, p.content, c.name as category_name, cp.title as category_title
                                FROM `sym_blog` as b
                                    LEFT JOIN `paragraphs` as p ON b.paragraph_id=p.id AND p.lng_id=".$kernel->lang->id."
                                    LEFT JOIN `sym_category` as c ON c.id=b.category_id
                                    LEFT JOIN `paragraphs` as cp ON c.paragraph_id=cp.id AND cp.lng_id=".$kernel->lang->id."
                                WHERE p.title!=''
                                ORDER BY date DESC
                                ".$lim);
        
        $this->notes='';
        $i=0;
        if(is_array($notes)){
            foreach($notes as $note){
                $this->category_name=$note['category_name'];
                $this->category_title=$note['category_title'];
                $this->date=$note['d'];
                $this->name=$note['name'];
                $this->title=$note['title'];
                if($type==0){
                    $this->content=data::html_remove($note['content'], $count);
                }
                elseif($type==1){
                    $this->content=data::paragraph($note['content'], $count);
                }
                else{
                    $this->content=$note['content'];
                }
                $this->i=$i;
                $this->notes.=design::run($this->note);
                $i++;
            }
        }
        
        $d->inc();
    }
    public function ___admin($design){
        global $kernel, $sym;
        $kernel->symbiont_add('blog-admin');
        $sym->blog_admin->___main('');
    }
    
    public function _administrate($symbiont){
        $design=$this->_check('administrate');
        $d=new design($design, 'blog', false);
        $this->categories='';
        $this->category();
        $d->inc();
    }
    private function category($id=0, $nbsp=''){
        global $sql, $kernel;
        $cat=$sql->query_each("SELECT c.name, c.id, p.title FROM `sym_category` as c LEFT JOIN `paragraphs` as p ON c.paragraph_id=p.id and p.lng_id = ".$kernel->lang->id." WHERE parent_id=".$id." ORDER BY position");
        if(is_array($cat)){
            foreach($cat as $val){
                $this->name=$val['name'];
                $this->title=$val['title']?$nbsp.$val['title']:$nbsp.$val['name'];
                $this->categories.=design::run($this->category);
                $this->category($val['id'], $nbsp.'&nbsp;');
            }
        }
    }
}
?>